package com.lpwa.web.controller.system;

import java.util.List;

import com.lpwa.common.utils.StringUtils;
import com.lpwa.framework.util.ShiroUtils;
import com.lpwa.system.domain.Person;
import com.lpwa.system.domain.Tag;
import com.lpwa.system.service.ILableService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.lpwa.common.annotation.Log;
import com.lpwa.common.enums.BusinessType;
import com.lpwa.common.core.controller.BaseController;
import com.lpwa.common.core.page.TableDataInfo;
import com.lpwa.common.core.domain.AjaxResult;
import com.lpwa.common.utils.poi.ExcelUtil;

/**
 * 标签 信息操作处理
 * 
 * @author tanmu
 * @date 2019-06-03
 */
@Controller
@RequestMapping("/system/lable")
public class LableController extends BaseController
{
    private String prefix = "system/lable";
	
	@Autowired
	private ILableService lableService;
	
	@RequiresPermissions("system:lable:view")
	@GetMapping()
	public String lable()
	{
	    return prefix + "/lable";
	}
	@RequiresPermissions("system:lable:view")
	@GetMapping("/{tagId}")
	public String lableByTagId(@PathVariable("tagId") String tagId ,ModelMap mmap)
	{
		mmap.put("tagId", tagId);
		return prefix + "/lable";
	}
	@RequiresPermissions("system:lable:view")
	@GetMapping("/queryLable")
	public String queryLable()
	{
		return prefix + "/queryLable";
	}
	
	/**
	 * 查询标签列表
	 */
	@RequiresPermissions("system:lable:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Tag lable)
	{
		startPage();
        List<Tag> list = lableService.selectLableList(lable);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出标签列表
	 */
	@RequiresPermissions("system:lable:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Tag lable)
    {
    	List<Tag> list = lableService.selectLableList(lable);
        ExcelUtil<Tag> util = new ExcelUtil<Tag>(Tag.class);
        return util.exportExcel(list, "lable");
    }
	
	/**
	 * 新增标签
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存标签
	 */
	@RequiresPermissions("system:lable:add")
	@Log(title = "标签", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Tag lable)
	{
		String tagId = lable.getTagId();
		lable.setTagId(tagId.toLowerCase());
//		String modelId = lable.getTagType();
//		if (StringUtils.isEmpty(modelId)){
//			throw new RuntimeException("标签Id格式不正确");
//		}
		lable.setCreateBy(ShiroUtils.getLoginName());
		return toAjax(lableService.insertLable(lable));
	}

	/**
	 * 修改标签
	 */
	@GetMapping("/edit/{lableId}")
	public String edit(@PathVariable("lableId") String lableId, ModelMap mmap)
	{
		Tag lable = lableService.selectLableById(lableId);
		mmap.put("lable", lable);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存标签
	 */
	@RequiresPermissions("system:lable:edit")
	@Log(title = "标签", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Tag lable)
	{
		lable.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(lableService.updateLable(lable));
	}
	
	/**
	 * 删除标签
	 */
	@RequiresPermissions("system:lable:remove")
	@Log(title = "标签", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(lableService.deleteLableByIds(ids));
	}
	/**
	 * 校验ID唯一
	 */
	@PostMapping("/checkLableIdUnique")
	@ResponseBody
	public String checkLableIdUnique(Tag lable)
	{
		return lableService.checkLableIdUnique(lable.getTagId());
	}
	/**
	 * 校验ID唯一
	 */
	@PostMapping("/getPersonInfoByTagId")
	@ResponseBody
	public AjaxResult getPersonInfoByTagId(@RequestParam("tagId") String tagId)
	{
		Person person = lableService.getPersonInfoByTagId(tagId);
		if (StringUtils.isNull(person)){
			return AjaxResult.success(new Person());
		}
		return AjaxResult.success(person);
	}
	/**
	 * 删除标签
	 */
	@RequiresPermissions("system:lable:remove")
	@Log(title = "标签", businessType = BusinessType.DELETE)
	@PostMapping( "/rpcToDevice")
	@ResponseBody
	public AjaxResult rpcToDevice(String ids)
	{

		return toAjax(lableService.deleteLableByIds(ids));
	}

}
